package com.siao.display
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 显示元素
	 * @author SiaoLeon
	 */
	public class DisplayElement extends Sprite
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function DisplayElement()
		{
			super();
			
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 从父级移除
		 */
		public function remove():void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		/**
		 * 清空子对象
		 */
		public function clear():void
		{
			while (this.numChildren > 0)
			{
				this.removeChildAt(0);
			}
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 添加到舞台时处理函数
		 */
		protected function onAddedToStage():void
		{
		}
		
		/**
		 * 从舞台移除时处理函数
		 */
		protected function onRemovedFromStage():void
		{
		}
		
		/**
		 * 添加到舞台时
		 * @param event
		 */
		internal function addedToStageHandler(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler, false, 0, true);
			
			this.onAddedToStage();
		}
		
		/**
		 * 从舞台移除时
		 * @param event
		 */
		internal function removedFromStageHandler(event:Event):void
		{
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
			
			this.onRemovedFromStage();
		}
	
	}
}
